package tmi.li.txwidgets.txwthr;



import java.io.StringReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import javax.xml.parsers.*;


public class WeatherUpdate {
	
	private String url = "http://weather.yahooapis.com/forecastrss?";
	public String current;
	public String wcode;
	public String desc;
	public String desc2;
	
	public void getRss(String woeid, String format) {
			try {
				HttpClient hc = new DefaultHttpClient();
		    	HttpGet test = new HttpGet(url+"w="+woeid+"&u="+format);
		    	HttpResponse rp = hc.execute(test);      	
		    	if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
		    	{
		    		String str = EntityUtils.toString(rp.getEntity());
		    		getInfo(str);
		    	}else {
		    	}

			}catch(Exception e) {
			}

	}
	
	public void getInfo(String str) {
			try {
				DocumentBuilder d = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				StringReader reader = new StringReader( str );
				InputSource inputSource = new InputSource( reader );
				Document doc = null ;
			    doc = d.parse( inputSource );
				reader.close();
				NodeList nodes = doc.getElementsByTagName("item").item(0).getChildNodes();
				Node node =  nodes.item(11);
				String weatherCode = node.getAttributes().getNamedItem("code").getNodeValue();
				String weatherTemp = node.getAttributes().getNamedItem("temp").getNodeValue();
				
				this.current = weatherTemp;
				this.wcode = weatherCode;

				
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}catch (Exception e){
			}
	}
	
	
	public boolean compareString(String orig, String cmp) {
		int orig_size = orig.length();
		int cmp_size = cmp.length();
		String orig_up = orig.toLowerCase();
		String orig_down = orig.toUpperCase();
		int b = 0;
		for(int i = 0; i < orig_size; i++) {
			if(orig.charAt(i) == cmp.charAt(b) || orig_up.charAt(i) == cmp.charAt(b) || orig_down.charAt(i) == cmp.charAt(b)) {
				b++;
				if(b == cmp_size)return true;
				continue;
			}
			else {
				if(orig_size - i < cmp_size) return false;
				if(b != 0) b = 0;
				continue;
			}
			
		}
		
		return false;
		
	}
	
	
}
